package piwan.editor;

import piwan.*;
import java.util.ArrayList;

import org.fenggui.Button;
import org.fenggui.CheckBox;
import org.fenggui.ComboBox;
import org.fenggui.Display;
import org.fenggui.FengGUI;
import org.fenggui.TextEditor;
import org.fenggui.composite.Window;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.event.ISelectionChangedListener;
import org.fenggui.event.ITextChangedListener;
import org.fenggui.event.SelectionChangedEvent;
import org.fenggui.event.TextChangedEvent;
import org.fenggui.layout.RowLayout;
import org.fenggui.util.Point;

@SuppressWarnings("deprecation")
public class EditorGui {
	public EditorGui(MainWindow main, Listener lst) {
		this.main = main;
		this.display = main.getDisplay();
		this.lst = lst;
		new MapWindow();
		display.layout();
	}
	
	public void setMapInfo( EditMap map ) {
		types = map.getRegionTypes();
		rscs = map.getResourceTypes();
		if( winreg != null )
			winreg.win.close();
		winreg = new RegionWindow();
		selectRegion(selPoly);
	}
	
	public void selectRegion(EditPoly sp) {
		selPoly = sp;
		if(winreg!=null) {
			if(sp!=null) {
				winreg.setEnabled(true);
				winreg.setRegion();
			} else
				winreg.setEnabled(false);
		}
	}
	
	public interface Listener {
		void newMap();
		void saveMap( String fileName );
		void openMap( String fileName );
		void importImage( String fileName );
		void newRegion();
		void connectionMode();
	}
	
	// helper funcs
	public RegionType getRegionType(String name) {
		RegionType found = null;
		for(RegionType r: types) {
			System.out.println("TEST: '" + name + "' '" + r.getName() + "'");
			if(r.getName().equals(name)) {
				found = r;
				break;
			}
		}
		if(found==null) throw new NullPointerException();
		assert found!=null : name;
		return found;
	}
	
	// Windows
	private class MapWindow {
		public MapWindow() {
			win = FengGUI.createWindow(display, false, false, false, false);
			win.setTitle("Map");
			win.setLayoutManager(new RowLayout(false));
			butnew	= FengGUI.createButton(win, "New Map");
			butsave	= FengGUI.createButton(win, "Save Map");
			butopen	= FengGUI.createButton(win, "Open Map...");
			butimg	= FengGUI.createButton(win, "Import Image...");
			
			butnew.addButtonPressedListener(new IButtonPressedListener() {
				public void buttonPressed(ButtonPressedEvent arg0) {
					lst.newMap();
				}
			});
			butsave.addButtonPressedListener(new IButtonPressedListener() {
				public void buttonPressed(ButtonPressedEvent arg0) {
					lst.saveMap("edit.map");
				}
			});
			butopen.addButtonPressedListener(new IButtonPressedListener() {
				public void buttonPressed(ButtonPressedEvent arg0) {
					lst.openMap("edit.map");
				}
			});
			butimg.addButtonPressedListener(new IButtonPressedListener() {
				public void buttonPressed(ButtonPressedEvent arg0) {
					lst.importImage("gfx/mapimage.png");
				}
			});
			
			win.pack();
		}
		
		private Window win;
		private Button butnew;
		private Button butsave;
		private Button butopen;
		private Button butimg;
	}
	
	private class RegionWindow {
		public RegionWindow() {
			win = FengGUI.createWindow(display, false, false, false, false);
			win.setTitle("Region");
			win.setLayoutManager(new RowLayout(false));
			
			buildTools();
			buildText();
			buildCombo();
			buildCheckBoxes();
			
			win.pack();
			win.setPosition(new Point(main.getWidth()-win.getWidth()-20,main.getHeight()-win.getHeight()-20));
		}
		
		public void setEnabled( boolean b ) {
			cmbtype.setEnabled(b);
			txtname.setEnabled(b);
			for(CheckBox<Integer> chk: chks)
				chk.setEnabled(b);	
		}
		
		public void setType( RegionType regType ) {
			cmbtype.setSelected(regType.getName());
		}
		
	
		public void setRegion() {
			EditPoly sv = selPoly;
			selPoly = null;
			txtname.setText(sv.getName());
			if(sv.getType()==null) 
				sv.setType(types.get(1));
			setType(sv.getType());
			
			for(CheckBox<Integer> chk: winreg.chks)
				chk.setSelected(false);
			
			for(ResourceType res: sv.getResources())
				chks.get(res.getId()).setSelected(true);
			selPoly = sv;
		}
		
		
		private void buildCheckBoxes() {
			chks = new ArrayList<CheckBox<Integer>>(rscs.size());
			for(ResourceType res: rscs) {
				CheckBox<Integer> chk = FengGUI.<Integer>createCheckBox(win,res.getName());
				chk.setValue(res.getId());
				chk.addSelectionChangedListener(new ISelectionChangedListener() {
						@SuppressWarnings("unchecked")
						public void selectionChanged(SelectionChangedEvent arg) {
							synchronized(EditorGui.this) {
								if(selPoly==null) return;
								CheckBox<Integer> w = (CheckBox<Integer>)arg.getToggableWidget();
								if(arg.isSelected()) {
									selPoly.addResource( rscs.get(w.getValue()) );
								}else
									selPoly.removeResource( rscs.get(w.getValue()) );
							}
						}
				});
				chks.add( chk );
			}
		}
		
		private void buildCombo() {
			cmbtype = FengGUI.createComboBox(win);
			for(RegionType type: types)
				cmbtype.addItem(type.getName());
			cmbtype.addSelectionChangedListener(new ISelectionChangedListener() {
				public void selectionChanged(SelectionChangedEvent arg) {
					if(selPoly!=null && arg.isSelected())
						selPoly.setType( getRegionType(cmbtype.getSelectedValue()) );
				}
			});
		}
		
		private void buildTools() {
			butnew	= FengGUI.createButton(win, "New Region...");
			butnew.addButtonPressedListener(new IButtonPressedListener() {
				public void buttonPressed(ButtonPressedEvent arg0) {
					lst.newRegion();
				}
			});
			
			butconnect = FengGUI.createButton(win,"Add Connections...");
			butconnect.addButtonPressedListener(new IButtonPressedListener() {
				public void buttonPressed(ButtonPressedEvent arg) {
					lst.connectionMode();
				}
			});
		
		}
		
		private void buildText() {
			txtname = FengGUI.createTextEditor(win, "<unnamed>");
			txtname.addTextChangedListener(new ITextChangedListener() {
				public void textChanged(TextChangedEvent arg) {
					if( selPoly == null ) return;
					String st = txtname.getText().replaceAll("\n", "");
					selPoly.setName(st);
				}
			});
		}
		
		private Window win = null;
		private Button butnew = null;
		private Button butconnect = null;
		private TextEditor txtname = null;
		private ComboBox cmbtype = null;
		private ArrayList< CheckBox<Integer> > chks = null;
		
	}	
	
	private MainWindow main = null;
	private Display display = null;
	private ArrayList<RegionType> types = null;
	private ArrayList<ResourceType> rscs = null;
	private EditPoly selPoly = null;
	private Listener lst = null;
	
	private RegionWindow winreg = null;
}
